package com.xuan.chatroom.mapper;


import com.xuan.chatroom.model.AddRequest;
import com.xuan.chatroom.model.Friend;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface FriendMapper {

    // 获取当前用户的好友列表
    List<Friend> getFriendList(int userId);


    // 插入好友申请记录
    Integer insertAddRequest(AddRequest addRequest);


    // 获取好友申请记录
    List<AddRequest> getAddRecords(Integer toId);


    // 互相添加好友
    @Insert("insert into friend values (#{userId}, #{friendId})")
    Integer addFriend(Integer userId, Integer friendId);

    // 处理好友请求，修改请求状态
    @Update("update add_request set valid_flag = #{validFlag}, request_message = #{requestMessage} where from_id = #{fromId} and to_id = #{toId}")
    void updateAddRequest(AddRequest addRequest);


    // 查看好友申请是否存在则有效(即fromId 和 toId 存在，且 validFlag = 0)
    @Select("select * from add_request where from_id = #{fromId} and to_id = #{toId} and valid_flag = 0")
    AddRequest isExist(AddRequest addRequest);


    @Select("select * from friend where user_id = #{fromId} and friend_id = #{toId}")
    Friend isFriend(AddRequest addRequest);
}
